//


#import "MB_MyTabBar.h"

//
@interface MB_MyTabBar ()

@property (nonatomic, strong) UIButton *publishButton;
@property (nonatomic, strong) NSMutableArray * buttonArray;
@end
@implementation MB_MyTabBar

//创建tabbar视图
- (id)initWithFrame:(CGRect)frame andTabBarButtonDictory:(NSDictionary *)dic andBlock:(MBSetIndexBlock)block{
    self = [super initWithFrame:frame];
    if (self) {
        _block = block;
        [self mbCreateTabBarWithDictory:dic];
        self.backgroundColor = rgba(255, 255, 255, 1);
        
    }
    return self;
}

//创建tabbar按钮
- (void)mbCreateTabBarWithDictory:(NSDictionary *)dic{
    if (dic.count == 0) {
        return;
    }
    //创建一个分割线
    UIView * divisionLine = [[UIView alloc]initWithFrame:CGRectMake(0,0, kMainScreenWidth, 0.5)];
    divisionLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:divisionLine];
    //创建按钮
    NSArray * imageNameNormal = [dic objectForKey:@"imageNameNormal"];
    NSArray * imageNameSelected = [dic objectForKey:@"imageNameSelected"];
    NSArray * buttonTitle = [dic objectForKey:@"buttonTitle"];
    CGFloat width = kMainScreenWidth/imageNameNormal.count;
    CGFloat imageHeight = 35;
    for (int i = 0 ; i< imageNameNormal.count ; i++) {
        //按钮
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(width*i, 7, width, imageHeight);
        [btn setImage:[UIImage imageNamed:imageNameNormal[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageNameSelected[i]] forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:imageNameSelected[i]] forState:UIControlStateHighlighted];
        [btn setTitle:buttonTitle[i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        [btn setTitleColor:rgba(142, 143, 142, 1) forState:UIControlStateNormal];
        [btn setTitleColor:rgba(245, 64, 106, 1) forState:UIControlStateSelected];
        btn.tag = 100 + i;
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height+5 ,-btn.imageView.frame.size.width, 0.0,0.0)];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(-5, 0.0,0.0, -btn.titleLabel.bounds.size.width)];
        
        if (i==0) {
            btn.selected = YES;
        }
        [btn addTarget:self action:@selector(actionClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [self.buttonArray addObject:btn];
        
        
    }
}

- (UIButton *)publishButton {
    if (!_publishButton) {
        _publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_publishButton setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
        [_publishButton setBackgroundColor:[UIColor clearColor]];
        // 发布按钮的点击事件
        [_publishButton addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_publishButton];
    }
    return _publishButton;
}


- (NSMutableArray *)buttonArray {
    if (_buttonArray == nil) {
        _buttonArray = [NSMutableArray new];
    }
    return _buttonArray;
}

/*
- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 设置所有UITabBarButton的frame /
    // 按钮的尺寸
    CGFloat buttonW = self.frame.size.width / 3;
    CGFloat buttonH = 49;
    CGFloat buttonY = 0;
    // 按钮索引
    int buttonIndex = 0;
    
    for (UIButton *subview in self.buttonArray) {
        // 过滤掉非UITabBarButton
        if (subview == self.publishButton) continue;
        
        // 设置frame
        CGFloat buttonX = buttonIndex * buttonW;
        // 把发布按钮的位置预留出来
        if (buttonIndex >= 1) { // 右边的2个UITabBarButton
            buttonX += buttonW;
        }
        subview.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        // 增加索引
        buttonIndex++;
    }
    //设置中间的发布按钮的frame /
    self.publishButton.frame = CGRectMake(0, 0, buttonH+0, buttonH+0);
    self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, (self.frame.size.height - 30 - HOME_INDICATOR_HEIGHT));//button超出
}

//判断点是否在响应范围
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.isHidden == NO) {
        UIBezierPath *circle  = [UIBezierPath bezierPathWithArcCenter:self.publishButton.center radius:35 startAngle:0 endAngle:2* M_PI clockwise:YES];
        UIBezierPath *tabbar  = [UIBezierPath bezierPathWithRect:self.bounds];
        if ( [circle containsPoint:point] || [tabbar containsPoint:point]) {
            return YES;
        }
        return NO;
    }else {
        return [super pointInside:point withEvent:event];
    }
}
//
- (void)Action {
    
    if ([self.delegate respondsToSelector:@selector(mbSpecailButtonClickAction)]) {
        [self.delegate mbSpecailButtonClickAction];
    }
}
//*/

//MARK:切换tabbar
- (void)actionClick:(UIButton *)btn{
    
    _block(btn.tag - 100);
    for (UIButton * b in self.subviews) {
        if ([b isKindOfClass:[UIButton class]]) {
            if (b.tag == btn.tag) {
                b.selected = YES;
            }else{
                b.selected = NO;
            }
        }
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
